package com.mocha.bass.test.jiangan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.util.JsonUtil;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/29 23:36
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class PointUpdate {

    @Autowired
    private PointPGRepository pointPGRepository;

    @Test
    public void test(){

        List<PointPGEntity> list = pointPGRepository.lambdaQuery().eq(PointPGEntity::getParkCode, "jiangan").list();
        Map<String, PointPGEntity> dataMap = list.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));


        String path = JsonUtil.class.getClassLoader().getResource("jiangan_hnew.json").getPath();
        String s = JsonUtil.readJsonFile(path);
        Map<String, String[]> map = JSON.parseObject(s, new TypeReference<Map<String, String[]>>() {});

        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            PointPGEntity point = new PointPGEntity();
            PointPGEntity oldE = dataMap.get(entry.getKey());
            if (null == oldE){
                point.setParkCode("jiangan");
                point.setPointNo(entry.getKey());
                point.setLon(entry.getValue()[0]);
                point.setLat(entry.getValue()[1]);
                point.setHeight(entry.getValue()[2]);
                pointPGRepository.save(point);
            }else {
                point.setId(oldE.getId());
                point.setLon(entry.getValue()[0]);
                point.setLat(entry.getValue()[1]);
                point.setHeight(entry.getValue()[2]);
                pointPGRepository.updateById(point);
            }

        }

    }



}
